package cn.edu.qzuie.artist.module.td.dal.mysql.publishactivity;

import java.util.*;

import cn.edu.qzuie.artist.framework.common.pojo.PageParam;
import cn.edu.qzuie.artist.framework.common.pojo.PageResult;
import cn.edu.qzuie.artist.framework.mybatis.core.query.LambdaQueryWrapperX;
import cn.edu.qzuie.artist.framework.mybatis.core.mapper.BaseMapperX;
import cn.edu.qzuie.artist.module.system.api.user.dto.AdminUserRespDTO;
import cn.edu.qzuie.artist.module.system.dal.dataobject.user.AdminUserDO;
import cn.edu.qzuie.artist.module.td.controller.admin.participateactivitiy.vo.ParticipateActivitiyPageReqVO;
import cn.edu.qzuie.artist.module.td.controller.admin.participateactivitiy.vo.ParticipateActivitiyPagesRespVO;
import cn.edu.qzuie.artist.module.td.dal.dataobject.activity.ActivityDO;
import cn.edu.qzuie.artist.module.td.dal.dataobject.participateactivitiy.ParticipateActivitiyDO;
import cn.edu.qzuie.artist.module.td.dal.dataobject.publishactivity.PublishActivityDO;
import com.github.yulichang.wrapper.MPJLambdaWrapper;
import org.apache.ibatis.annotations.Mapper;
import cn.edu.qzuie.artist.module.td.controller.admin.publishactivity.vo.*;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;

/**
 *  发布活动 Mapper
 *
 * @author 泉信管理员
 */
@Mapper
public interface PublishActivityMapper extends BaseMapperX<PublishActivityDO> {

    default PageResult<PublishActivityDO> selectPage(PublishActivityPageReqVO reqVO) {
        return selectPage(reqVO, new LambdaQueryWrapperX<PublishActivityDO>()
                .likeIfPresent(PublishActivityDO::getActivityName, reqVO.getActivityName())
                .likeIfPresent(PublishActivityDO::getNickName, reqVO.getNickname())
                .eqIfPresent(PublishActivityDO::getUserType, reqVO.getUserType())
                .orderByDesc(PublishActivityDO::getId));
    }

    default PageResult<PublishActivityPagesRespVO> selectPageWithName(PageParam pageParam, PublishActivityPageReqVO reqVO) {
        // 构建查询条件
        MPJLambdaWrapper<PublishActivityDO> wrapper = new MPJLambdaWrapper<>();

        // 查询td_activity 表和 td_admin_user 表
        wrapper.selectAll(PublishActivityDO.class)
                // 查询
                .select(ActivityDO::getActivityName, ActivityDO::getId)
                .select(AdminUserDO::getNickname, AdminUserDO::getId)
                .leftJoin(ActivityDO.class, ActivityDO::getId, PublishActivityDO::getActivityId)
                .leftJoin(AdminUserDO.class, AdminUserDO::getId, PublishActivityDO::getUserId);
        // 动态拼接查询条件
        if (reqVO.getActivityId() != null) {
            wrapper.eq(PublishActivityDO::getActivityId, reqVO.getActivityId()); // 精确匹配活动 ID
        }
        if (reqVO.getUserId() != null) {
            wrapper.eq(PublishActivityDO::getUserId, reqVO.getUserId()); // 精确匹配用户 ID
        }
        if (reqVO.getUserType() != null ) {
            wrapper.eq(PublishActivityDO::getUserType, reqVO.getUserType());  // 用户类型
        }

        // 默认排序：按 创建时间 倒序
        wrapper.orderByAsc(PublishActivityDO::getCreateTime);
        // 执行查询并返回结果
        //PageResult<PublishActivityPagesRespVO> pageResult =
        return selectJoinPage(pageParam, PublishActivityPagesRespVO.class, wrapper);

    }
}